package lib.common.use;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ScrollView;

import com.app.BaseActivity;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

// http://blog.csdn.net/ueryueryuery/article/details/17440465
//
public class RefreshActivity extends BaseActivity {

	private RefreshActivity activity = this;

	private ArrayList<String> listdata = new ArrayList<String>();

	private PullToRefreshScrollView scroll_view;

	@Override
	public int InitLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_refresh;
	}

	@Override
	public void InitView() {
		// TODO Auto-generated method stub

		for (int i = 0; i < 3; i++) {
			listdata.add("" + i);
		}

		scroll_view = (PullToRefreshScrollView) findViewById(R.id.scroll_view);
//		scroll_view.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
//
//			@Override
//			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
//				// TODO Auto-generated method stub
//				
//				new GetDataTask().execute();
//
//			}
//		});
		
		scroll_view.setOnRefreshListener( new OnRefreshListener2<ScrollView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ScrollView> refreshView) {
				// TODO Auto-generated method stub
				new GetDataTask().execute();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ScrollView> refreshView) {
				// TODO Auto-generated method stub
				new GetDataTask().execute();
			}
		});
		
		scroll_view.getLoadingLayoutProxy(false, true).setPullLabel("加载更多setPullLabel");
		scroll_view.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载更多setRefreshingLabel");
		scroll_view.getLoadingLayoutProxy(false, true).setReleaseLabel("加载更多setReleaseLabel");

		
		Drawable anim = getResources().getDrawable(R.anim.loading);
		scroll_view.getLoadingLayoutProxy(false, true).setLoadingDrawable(anim);
		
		
	}

	@Override
	public void InitListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void InitProcess() {
		// TODO Auto-generated method stub

	}
	
	
	

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(4000);
			} 
			catch (InterruptedException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			// Do some stuff here
			// Call onRefreshComplete when the list has been refreshed.
			scroll_view.onRefreshComplete();
			super.onPostExecute(result);
		}

	}
	
	

}
